package com.youyun56.gps.android;

import java.util.LinkedList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.youyun56.gps.android.pojo.GoodInfo;
import com.youyun56.gps.android.service.LocationUploadService;

public class GoodInfoItemAdapter extends BaseAdapter {
    private GoodInfoActivity      context;
    private LinkedList<GoodInfo>  goodInfos = new LinkedList<GoodInfo>();
    public static int             max       = 200;
    private LocationUploadService service;
    
    public GoodInfoItemAdapter(GoodInfoActivity context) {
        service = LocationUploadService.getInstance();
        this.context = context;
    }
    
    @Override
    public int getCount() {
        return goodInfos.size();
    }
    
    @Override
    public Object getItem(int position) {
        return goodInfos.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.gooditem, null);
            view.requestFocusFromTouch();
        }
        final GoodInfo goodInfo = goodInfos.get(position);
        TextView msg = (TextView) view.findViewById(R.id.msg);
        msg.setText(goodInfo.getMsg());
        TextView contact = (TextView) view.findViewById(R.id.contact);
        contact.setText(goodInfo.getContact());
        TextView addTime = (TextView) view.findViewById(R.id.addTime);
        addTime.setText(goodInfo.getAddTime());
        return view;
    }
    
    public void addLast(GoodInfo goodInfo) {
        setGoodIndex(goodInfo);
        goodInfos.addLast(goodInfo);
        notifyDataSetChanged();
        context.getListView().requestFocusFromTouch();
    }
    
    private void setGoodIndex(GoodInfo goodInfo) {
        if (goodInfo.getIndex() > service.getCar().getGoodIndex()) {
            service.getCar().setGoodIndex(goodInfo.getIndex());
        }
    }
    
    public GoodInfo getLast() {
        if (goodInfos.size() > 0) {
            return goodInfos.get(goodInfos.size() - 1);
        } else {
            return null;
        }
    }
    
    public void addFirst(GoodInfo goodInfo) {
        setGoodIndex(goodInfo);
        if (goodInfos.size() > max) {
            goodInfos.removeLast();
        }
        goodInfos.addFirst(goodInfo);
        notifyDataSetChanged();
        context.getListView().requestFocusFromTouch();
    }
    
    public GoodInfo getFirst() {
        if (goodInfos.isEmpty()) {
            return null;
        } else {
            return goodInfos.get(0);
        }
    }
    
    public void remove(int position) {
        goodInfos.remove(position);
        notifyDataSetChanged();
    }
    
    public void removeAll() {
        goodInfos.clear();
        notifyDataSetChanged();
    }
    
}
